package com.market.cash.utils.dialog

import android.Manifest
import android.content.Context
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import com.market.cash.R
import com.market.cash.databinding.CashPhotoDialogBinding
import com.market.cash.pro.ProDialog
import com.market.cash.utils.event.CashRootEventHelper
import com.market.cash.utils.event.CashRootEventType
import com.market.cash.utils.permission.OnPermissionCallback
import com.market.cash.utils.permission.XXPermissions

/**
 * CashRoot
 * @Author wwdeng
 * @CreateTime: 2024/10/22 17:59
 * @Annotation：拍照、相册选择 dialog
 */
class CashRootPhotoDialog: ProDialog {
    private lateinit var binding: CashPhotoDialogBinding
    private var mCallback: IDialogCallback? = null
    var selectType = -1
    //事件备注
    private var eventRemark = ""

    constructor(context: Context) : super(context) {}
    constructor(context: Context, callback: IDialogCallback?) : super(context) {
        mCallback = callback
    }

    constructor(context: Context, remark: String,callback: IDialogCallback?) : super(context) {
        mCallback = callback
        eventRemark = remark
    }

    override fun initWindow() {
        val params = window!!.attributes
        params.width =  WindowManager.LayoutParams.MATCH_PARENT
        params.height = WindowManager.LayoutParams.WRAP_CONTENT
        window!!.attributes = params
        window!!.setWindowAnimations(R.style.dialog_slide_anim) //修改动画样式
        window!!.setGravity(Gravity.BOTTOM)
    }

    /**
     * 设置备注
     */
    fun setEventRemark(remark: String){
        eventRemark = remark
    }

    /**
     * 设置点击回调
     */
    fun setOnClick(callback: IDialogCallback){
        this.mCallback = callback
    }

    override fun getRootView(): View {
        binding = CashPhotoDialogBinding.inflate(LayoutInflater.from(context))
        return binding.root
    }

    override fun initView() {
        selectType = -1
        checkCashRootType()
    }

    override fun initListener() {
        setOnClickListener(binding.imgClose)//关闭
        setOnClickListener(binding.btnConfirm)//确认
        //拍照
        binding.cameraLayout.setOnClickListener {
            selectType = 1
            checkCashRootType()
        }
        //相册选择
        binding.albumLayout.setOnClickListener {
            selectType = 2
            checkCashRootType()
        }
    }

    override fun initData() {

    }

    override fun onViewClick(v: View?) {
        when(v!!.id){
            R.id.btnConfirm -> {
                if (selectType == 1){
                    showCashRootCameraClick()
                    dismiss()
                    return
                }
                if (selectType == 2){
                    showCashRootAlbumClick()
                    dismiss()
                    return
                }
            }
            else -> {
                dismiss()
            }
        }
    }

    /**
     * 切换
     */
    private fun checkCashRootType(){
        binding.imgSelectCamera.setImageResource(R.mipmap.cash_photo_normal)
        binding.imgSelectAlbum.setImageResource(R.mipmap.cash_photo_normal)
        when(selectType){
            1 -> {
                binding.imgSelectCamera.setImageResource(R.mipmap.cash_photo_select)
            }
            2 -> {
                binding.imgSelectAlbum.setImageResource(R.mipmap.cash_photo_select)
            }
        }
    }

    /**
     * 拍照
     */
    private fun showCashRootCameraClick(){
        val permissions = arrayListOf(
            Manifest.permission.CAMERA
        )
        if (XXPermissions.isGranted(context,permissions)){
            if(mCallback != null){
                dismiss()
                mCallback!!.openCamera()
            }
        } else {
            XXPermissions.with(context)
                // 申请多个权限
                .permission(permissions)
                .request(object: OnPermissionCallback {
                    override fun onGranted(p0: MutableList<String>, p1: Boolean) {
                        var permissionString = ""
                        p0.forEach {
                            permissionString = "$permissionString$it,"
                        }
                        CashRootEventHelper.addCashRootRequestEventAction(CashRootEventType.CLICK.CASHROOT_CLICK_PERMISSION_SYSTEM_AGREE,permissionString.substring(0,permissionString.length - 1))
                        if(mCallback != null){
                            dismiss()
                            mCallback!!.openCamera()
                        }
                    }
                    override fun onDenied(p0: MutableList<String>, doNotAskAgain: Boolean) {
                        var permissionString = ""
                        p0.forEach {
                            permissionString = "$permissionString$it,"
                        }
                        CashRootEventHelper.addCashRootRequestEventAction(CashRootEventType.CLICK.CASHROOT_CLICK_PERMISSION_SYSTEM_DENY,permissionString.substring(0,permissionString.length - 1))
                        XXPermissions.startPermissionActivity(context,permissions)
                    }
                })
        }
    }

    /**
     * 相册
     */
    private fun showCashRootAlbumClick(){
        if(mCallback != null){
            dismiss()
            mCallback!!.openAlbum()
        }
    }

    interface IDialogCallback {
        //打开相机
        fun openCamera()
        //打开相册
        fun openAlbum()
    }

    override fun onStart() {
        super.onStart()
        CashRootEventHelper.addCashRootRequestEventAction(CashRootEventType.DIALOG.CASHROOT_DIALOG_PHOTO_SHOW,eventRemark)
    }

    override fun dismiss() {
        super.dismiss()
        CashRootEventHelper.addCashRootRequestEventAction(CashRootEventType.DIALOG.CASHROOT_DIALOG_PHOTO_DISMISS,eventRemark)
    }
}